package com.mszlu.news.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.news.admin.mapper.TagMapper;
import com.mszlu.news.admin.model.params.TagParam;
import com.mszlu.news.admin.pojo.Tag;
import com.mszlu.news.admin.service.TagService;
import com.mszlu.news.admin.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Override
    public Result selectTag(TagParam tagParam) {
        Page<Tag> page = new Page<>(tagParam.getPage(), tagParam.getPageSize());
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        if (tagParam.getTagName() != null) {
            tagQueryWrapper.like("tagName", tagParam.getTagName());
        }
        if (tagParam.getId() != null) {
            tagQueryWrapper.like("id", tagParam.getId());
        }
        Page<Tag> tagIPage = tagMapper.selectPage(page, tagQueryWrapper);
        return Result.success(tagIPage);
    }

    @Override
    public Result insertTag(TagParam tagParam) {
        Tag tag=new Tag();
        tag.setAvatar(tagParam.getAvatar());
        tag.setTagName(tagParam.getTagName());
        tagMapper.insert(tag);
        return Result.success("添加标签成功");
    }

    @Override
    public Result updateTag(TagParam tagParam) {
        Tag tag=new Tag();
        tag.setId(tagParam.getId());
        tag.setAvatar(tagParam.getAvatar());
        tag.setTagName(tagParam.getTagName());
        tagMapper.updateById(tag);
        return Result.success("更新标签成功");
    }

    @Override
    public Result deleteTagById(Long tagId) {
        tagMapper.deleteById(tagId);
        return Result.success("删除标签成功");
    }
}
